<template>
	<view class="login-container">
		<!-- 提示登录图标 -->
		<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
		<!-- 登录按钮 -->
		<button type="primary" class="btn-login" @tap="getUserProfile">一键登录</button>
		<view class="tips-text">
			登录后尽享更多权益
		</view>
	</view>
</template>

<script>
	import { mapMutations, mapState } from 'vuex'
	export default {
		name:"my-login",
		data() {
			return {
			};
		},
		computed: {
			...mapState('m_user', ['redirectinfo'])
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
			 getUserProfile(e) {
				 wx.getUserProfile({
					 desc: '完善资料',
					 success: (res) => {
						this.updateUserInfo(res.userInfo)
						this.getToken(res)
					 },
					 fail: (res) => {
						if (res.errMsg === 'getUserProfile:fail auth deny') {
							uni.$showMsg('您取消了授权')
						}		 
					 }
				 })
			 },
			 async getToken(info) {
				
				 // 获取 code对应值
				 const [err, res] = await uni.login().catch(err => err)
				 if (err || res.errMsg !== 'login:ok') {
					 uni.$showMsg("登录失败")
				 }
				 const query = {
					 code: res.code,
					 encryptedData: info.encryptedData,
					 iv: info.iv,
					 errMsg: info.errMsg,
					 signature: info.signature 
				 }
				 // 换取 token
				 const  token = 'lskfjsdlkfslfkdfkgj'
				 this.updateToken(token)
				 this.navigateBack()
				 
				 // const { data: loginResult } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
			 },
			 // 返回登录前的页面
			 navigateBack() {
				 // 如果redirectInfo不为null 且 导航方式为switchTab
				 if (this.redirectinfo && this.redirectinfo.openType === 'switchTab') {
					 // 调用小程序提供的api
					 console.log(this.redirectinfo)
					 uni.switchTab({
					 	url: '/page/cart/cart',
						complete: () => {
							// 导航完成之后重置vuex中的redirectinfo
							this.updateRedirectInfo(null)
						}
					 })
				 }
			 }
		}
	}
</script>

<style lang="scss">
	.login-container {
		height:  750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #F8F8F8;
		position: relative;
		overflow: hidden;
		
		&::after {
			content: ' ';
			background-color: #FFFFFF;
			position: absolute;
			width: 100%;
			height: 40px;
			bottom: 0;
			left: 0;
			border-radius: 100%;
			transform: translateY(50%)
		}
		.btn-login {
			width: 90%;
			background-color: #FA516B;
			border-radius: 40px;
			margin-top: 10px;
		}
		.tips-text {
			margin-top: 10px;
			font-size: 12px;
			color: gray;
		}
	}
</style>
